package common

import (
	"context"
	"device-admin/internal/app/grpc/client"
	"device-admin/internal/app/grpc/protoc/system_admin"
	"strings"

	"192.168.1.75/go-pkg/errorx"
)

// GetMerchantInfo 获取商户信息
func GetMerchantInfo(ctx context.Context, merchantID string) (merchantPath string, merchantName string, err error) {
	// 商户不能为空
	if merchantID == "" {
		return "", "", errorx.New("无效的商户", -1)
	}
	// 当为平台商户时，直接返回
	if merchantID == "1" {
		return ">1", "", nil
	}
	cli, err := client.GetSystemAdminClient()
	if err != nil {
		return "", "", errorx.New("获取商户信息失败", -1)
	}
	rsp, err := cli.MerchantInfo(ctx, &system_admin.MerchantInfoReq{
		MerchantId: merchantID,
	})
	if err != nil || rsp == nil || rsp.Code != 0 {
		return "", "", errorx.New("获取商户信息失败", -1)
	}
	return strings.Join([]string{rsp.MerchantPath, merchantID}, ">"), rsp.MerchantName, nil
}
